package com.clwl.supervise.superviseapp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {



    //注入刚刚配置类实例化的线程数学方面的参数类：
    @Autowired
    private ThreadPoolProperties poolProperties;

    @Bean()
    public ThreadPoolExecutor threadPoolExecutor() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(poolProperties.getCorePoolSize(),//核心线程池数
                poolProperties.getMaximumPoolSize(), //最大线程数
                poolProperties.getKeepAliveTime(), //线程挂载时长
                TimeUnit.SECONDS,//时长单位   （这里用秒）
                new ArrayBlockingQueue<>(poolProperties.getQueue()));//允许排队的队列数，程序阻塞数
        executor.allowCoreThreadTimeOut(true);//挂在时间到了仍然没有程序使用线程就允许线程挂掉。
        return executor;
    }

}

